package com.huluobo.a2107mvpimdemo.addGroup.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.adapter.AddGroupAdapter;
import com.huluobo.a2107mvpimdemo.addGroup.presenter.AddGroupContract;
import com.huluobo.a2107mvpimdemo.addGroup.presenter.AddGroupPresenter;
import com.huluobo.a2107mvpimdemo.base.BaseActivity;
import com.huluobo.a2107mvpimdemo.chat.ChatActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseActivity<AddGroupContract.View, AddGroupPresenter>
        implements AddGroupContract.View {

    private EditText groupName, groupDesc;

    private RecyclerView recyclerView;

    private List<EMGroupInfo> groupInfoList = new ArrayList<>();

    private AddGroupAdapter adapter;

    private String groupId;

    private Button addGroup_btn_findGroup, addGroup_btn_createGroup;

    @Override
    public void onCreateGroupSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddGroupActivity.this, "创建群成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onCreateGroupFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddGroupActivity.this, "创建群失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onFindGroupSuccess(List<EMGroupInfo> groupList) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                groupInfoList.clear();
                groupInfoList.addAll(groupList);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onFindGroupFailed(String error) {
        Toast.makeText(this, "查找群错误:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onJoinGroupSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AddGroupActivity.this, "加群成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onJoinGroupFailed(String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (error.equals("601")) {
                    //已经加入过该群,直接跳转到聊天页面
                    String conversationId = groupId;
                    Intent intent = new Intent(AddGroupActivity.this, ChatActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("conversationId", conversationId);
                    bundle.putInt("chatType", 2);
                    intent.putExtras(bundle);
                    AddGroupActivity.this.startActivity(intent);
                } else {
                    Toast.makeText(AddGroupActivity.this, "加群失败" + error, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void initData() {
        presenter = new AddGroupPresenter();
    }

    @Override
    protected void initView() {
        groupName = findViewById(R.id.addGroup_et_groupName);
        groupDesc = findViewById(R.id.addGroup_et_groupDesc);

        recyclerView = findViewById(R.id.addGroup_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddGroupAdapter(groupInfoList);
        recyclerView.setAdapter(adapter);

        addGroup_btn_findGroup = findViewById(R.id.addGroup_btn_findGroup);
        addGroup_btn_findGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.findGroup(100, null);
            }
        });
        addGroup_btn_createGroup = findViewById(R.id.addGroup_btn_createGroup);
        addGroup_btn_createGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EMGroupOptions emGroupOptions = new EMGroupOptions();
                emGroupOptions.maxUsers = 100;
                emGroupOptions.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                Log.i("TagA", "name:" + groupName.getText().toString() + ",desc:" + groupDesc.getText().toString());
                presenter.createGroup(groupName.getText().toString(), groupDesc.getText().toString(),
                        new String[]{EMClient.getInstance().getCurrentUser()}, "这是创建群", emGroupOptions);
            }
        });

        adapter.setOnAddClick(new AddGroupAdapter.OnAddClick() {
            @Override
            public void addClick(String groupId) {
                AddGroupActivity.this.groupId = groupId;
                presenter.joinGroup(groupId);
            }
        });

    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_add_group;
    }
}